//
//  LogDetailController.m
//  FreeTrip
//
//  Created by 邓芮 on 15/11/24.
//  Copyright © 2015年 邓芮. All rights reserved.
//

#import "LogDetailController.h"
#import "LogDetailTableCell.h"
@interface LogDetailController ()
@property (nonatomic, strong) NSMutableArray *modelArray;
@property (nonatomic, assign) NSInteger moreNum;//加载更多
@property (nonatomic, strong) UILabel *loadLabel;
@end

static NSString *cellIdentifier = @"cell";
@implementation LogDetailController


- (NSMutableArray *)modelArray
{
    if (!_modelArray )
    {
        self.modelArray = [NSMutableArray array];
    }
    return _modelArray;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerClass:[LogDetailTableCell class] forCellReuseIdentifier:cellIdentifier];
    self.moreNum = 0;
    NSString *url =[NSString stringWithFormat:KLogDetail,(long)_topic_id,(long)_moreNum];
   // NSLog(@"%@",url);
    [self netRequestForLogDetailWithUrl:url];
    

     
     self.tableView.separatorStyle = 0;
    
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        self.moreNum = self.moreNum + 1;
        [self netRequestForLogDetailWithUrl:[NSString stringWithFormat:KLogDetail,(long)_topic_id,(long)_moreNum]];

        [self.tableView.mj_footer endRefreshing];
    }];
    
    self.tableView.mj_footer = footer;
    
    if (![[NetworkState shareInstance]reachablity])
    {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    label.center = self.view.center;
    label.text = @"单击页面重新加载";
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
    self.loadLabel = label;
    }
    
    if (self.modelArray.count == 0)
    {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    
    [self.tableView addGestureRecognizer:tap];
    }
    
    
    
}

//http://api.lis99.com/v2/club/replylist/951930/?page=0?page=0&topic_id=951930
- (void)netRequestForLogDetailWithUrl:(NSString *)url
{
    
    
    [[NetworkState shareInstance] reachablityStates:self];
    [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
      {
          
          if (data == nil)
          {
              NSLog(@"数据请求失败");
                       
              return ;
          }
          
          
          NSDictionary *zomDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
          NSDictionary *firDic = zomDic[@"data"];
          for (NSDictionary *secDic in firDic[@"topiclist"])
          {
              LogDetailModel *model = [[LogDetailModel alloc] init];
              [model setValuesForKeysWithDictionary:secDic];
              NSArray *array = secDic[@"topic_image"];
              if (array.count >0)
              {
                   NSDictionary *thrDic = array[0];
                  [model setValuesForKeysWithDictionary:thrDic];
              }
             
              [_modelArray addObject:model];
              
          }
          
         
          dispatch_async(dispatch_get_main_queue(), ^{
              [self.tableView reloadData];
              [MBProgressHUD hideHUDForView:self.view animated:YES];
              [self.loadLabel removeFromSuperview];
          });
          
          
      }] resume];
    
}

- (void)tapAction:(UITapGestureRecognizer *)tap
{
    if (self.modelArray.count == 0)
    {
        NSString *url =[NSString stringWithFormat:KLogDetail,(long)_topic_id,(long)_moreNum];
       [self netRequestForLogDetailWithUrl:url];
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LogDetailTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    LogDetailModel * model = self.modelArray[indexPath.row];
    cell.model = model;

    
    return cell;
}
#pragma mark ---返回高度----
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LogDetailModel * model = self.modelArray[indexPath.row];
    return [LogDetailTableCell heightForRowWithModel:model] +10;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
